﻿define([
    'dojo/_base/declare',
    './../../one-map/1.0.0/src/mode/arcgis/esri/layers/PolygonLayer,
], function (declare, PolygonLayer) {
    var CircleLayer = declare([PolygonLayer], {
        setData: function (mapView, layer) {
            var me = this;
            me.clear();
            if (!mapView) {
                return;
            }
            me.mapView = mapView;
            var mapUtils = mapView.mapUtils;
            me._setLayer(layer);
            var options = {
                useEsriService: true,
                options: { units: 'kilometers', step: 64 },
                ...layer.layer,
            };
            me.options = options;
            var data = $.extend(true, [], layer.layer.data);

            me.highlightOptions = Object.assign(
                {
                    visible: false,
                    symbol: {
                        type: 'simplePolygon', //面样式类型
                        style: 'solid', //面填充类型
                        color: [0, 255, 255, 50], //面填充色
                        outline: {
                            color: [0, 255, 255], //边界颜色
                            style: 'solid', //边界线型,如果不设置，默认是solid
                            width: 2, //边界宽度
                        },
                    },
                },
                options.highlightOptions
            );

            me.popupOptions = Object.assign(
                {
                    visible: false,
                    isMultiPopup: false,
                    contentField: 'content',
                    isShowCloseIcon: false,
                },
                options.popupOptions
            );

            me.__currentGraphics = [];
            if (me.__mapOnLoadHandle) {
                clearInterval(me.__mapOnLoadHandle);
            }
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
            var spatialReference = me.spatialReference;
            me.__mapOnLoadHandle = setInterval(function () {
                if (mapView._isMapOnLoad) {
                    clearInterval(me.__mapOnLoadHandle);
                    if (!data || data.length < 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    } else {
                        var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
                        lngLatArray.map(function (item, index) {
                            if (mapView.geometryService && options.useEsriService) {
                                mapUtils.createBuffer(mapView, {
                                    distances: [options.radius],
                                    geometryProtContent: item,
                                    sr: spatialReference,
                                    callback: function (e) {
                                        item.rings = e[0].rings;
                                        fn(item, index);
                                    },
                                });
                            } else {
                                item.rings = mapUtils.turf.transformation.circle_sync(
                                    mapView,
                                    item,
                                    options.radius,
                                    options.options
                                );
                                fn(item, index);
                            }
                        });

                        me._showHighlight(mapView, options);
                    }
                }
            });

            function fn(item, index) {
                var polygon = mapUtils.createPolygon(mapView, item, { sr: spatialReference });
                var symbol = item[symbolFieldName] || options.symbol || 'common_polygon';
                mapUtils.getSymbol(mapView, symbol, function (symbol) {
                    var attributes = item;
                    if (!item.x || !item.y) {
                        var center = polygon && polygon.getCentroid();
                        if (center) {
                            item.x = center.x;
                            item.y = center.y;
                        }
                    }
                    var graphic = mapUtils.createGraphic(mapView, {
                        geometry: polygon,
                        symbol: symbol,
                        attributes: attributes,
                    });
                    me.add(graphic);

                    if (index == data.length - 1) {
                        me._calCurrentGraphics();

                        if (options.labelOptions) {
                            me.labelOptions = mapUtils.$.extend(
                                true,
                                {
                                    textField: 'name',
                                    ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                                    showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                                    scaleInfo: { maxScale: 0, minScale: 0 },
                                    labelSymbol: {
                                        labelClass: 'labelInfo',
                                        labelStyle: { offsety: 18, offsetx: 0, position: 'top' },
                                    },
                                },
                                options.labelOptions
                            );
                            me._showLabels(mapView, options);
                        }
                        if (options.popupOptions && options.popupOptions.visible) {
                            me.openCustomPopup(mapView, data);
                        }
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                });
            }
        },
    });
    return CircleLayer;
});
